import {
	uniHide,
	uniLoading,
	uniLinkTo,
	uniBack,
	uniToast
} from "./uniPromise";
import {
	resPost,
	adminPost,
	UpdateModifyData,
	AddNewData,
	GetQueryDataList,
	DeleteManagerData
} from "../common/http.request";
import store from "../store";
import qs from 'qs'

const install = (Vue) => {
	Vue.prototype.$loading = uniLoading
	Vue.prototype.$hide = uniHide
	Vue.prototype.$linkTo = linkToByCheckTab
	Vue.prototype.uniBack = uniBack
	Vue.prototype.$resPost = resPost
	Vue.prototype.$adminPost = adminPost
	Vue.prototype.$GetQueryDataList = GetQueryDataList
	Vue.prototype.$DeleteManagerData = DeleteManagerData
	Vue.prototype.$AddNewData = AddNewData
	Vue.prototype.$UpdateModify = UpdateModifyData
	Vue.prototype.$toast = uniToast
	Vue.prototype.$formatImg = formatImg
	Vue.prototype.$formatRpx = formatRpx
	Vue.prototype.$formatPx = formatPx
	Vue.prototype.$price = formatPrice
	Vue.prototype.$rebate = checkRebate
	Vue.prototype.$rebateNum = rebateNum
	Vue.prototype.$vipPrice = vipPrice
}

/**
 * 路由跳转，如果是tabBar页面，url传的值不能是路径，而是底部栏列表的url的值
 * 			判断为底部栏页，会把参数保存到 vuex_tab_params，由底部栏页自行处理
 * type 路由跳转方式： navigateTo switchTab reLaunch redirectTo
 * */
export function linkToByCheckTab(url, type = "navigateTo") {
	if (!url) return
	let path = url.split('?')[0]
	let index = store.state.vuex_tab_bar.list.findIndex(item => {
		return path === item.url
	})
	if (index > -1) {
		// 跳转的地址是tabBar页
		store.commit('$uStore', {
			name: 'vuex_tab_bar.current',
			value: index
		})
		if (url.includes('?')) {
			//路由中携带参数
			let [tabUrl, tabParams] = url.split('?')
			store.commit('$uStore', {
				name: 'vuex_tab_params',
				value: qs.parse(tabParams)
			})
			uniLinkTo(store.state.vuex_tab_page[index], 'switchTab')
		} else {
			uniLinkTo(store.state.vuex_tab_page[index], 'switchTab')
		}
	} else {
		uniLinkTo(url, type)
	}
}

/**
 * @param img 图片链接
 * @param errorUrl
 */
function formatImg(img, errorUrl = '') {
	if (img) {
		if (img.indexOf('http') > -1) {
			return img
		} else {
			return store.state.vuex_businessObj.resquest + img
		}
	} else {
		return errorUrl
	}
}

//计算返佣金额
// rebate 商品价格返佣    commission 返佣比例
function rebateNum(rebate, commission) {
	if (commission) {
		rebate = parseFloat(commission) * parseFloat(rebate) / 100
		return priceFmt(rebate)
	} else {
		return priceFmt(rebate)
	}
}

function priceFmt(price) {
	if (price) {
		price = parseFloat(price)
		var intPrice = parseInt(price)
		if (price - intPrice > 0) {
			return price.toFixed(2)
		} else {
			return intPrice
		}
	} else {
		return 0
	}
}

function formatRpx(val) {
	return uni.upx2px(val)
}

function formatPx(val) {
	if(val){
		return val / (uni.upx2px(val) / val)
	}else{
		return 0
	}
}

function formatPrice(price = 0) {
	if (price) {
		price = parseFloat(price)
		var intPrice = parseInt(price)
		if (price - intPrice > 0) {
			return price.toFixed(2)
		} else {
			return intPrice + ''
		}
	} else {
		return '0'
	}
}


// 判断是会员还是正常价格
function vipPrice(price, vipPrice) {
		if (store.state.vuex_user && store.state.vuex_user.type == 1) {
			return vipPrice
		} else {
			return price
		}
	}

	function checkRebate(rebate) {
		let type = store.state.vuex_user.type
		if (type == '2') {
			if (rebate && parseFloat(rebate) > 0) {
				return true
			} else {
				return false
			}
		} else {
			return false
		}
	}


export default {
	install
}
